/*
 * Copyright (c) 2015 MONKEYK Information Technology Co. Ltd
 * www.monkeyk.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * MONKEYK Information Technology Co. Ltd ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you
 * entered into with MONKEYK Information Technology Co. Ltd.
 */
package com.itic.im.core.jdbc;

import com.itic.im.core.model.ImMsg;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * date: 2022/1/19 15:06
 * author: yangwl
 */
public class ImMsgRowMapper implements RowMapper<ImMsg> {


    public ImMsgRowMapper() {
    }

    // SELECT ID, CHAT_TYPE, SEND_UID, RECEIVE_UID, GROUP_ID, MSG_TYPE, MSG, IF_WITHDRAW, MSG_SHARDING_DATE, `TIME`, MSG_EXT, MSG_SEQ, FILE_NAME, FILE_TYPE,
    // FILE_SIZE, FILE_EXTENSION, FILE_LASTMODIFIED, FILE_LASTMODIFIEDDATE, REFERENCE_MSG_ID, EXT1, EXT2, EXT3, EXT4, EXT5, EXT6
    //FROM itic.t_im_msg;
    @Override
    public ImMsg mapRow(ResultSet rs, int i) throws SQLException {
        ImMsg imMsg = new ImMsg();
        imMsg.setId(getLong(rs,"ID"));
        imMsg.setChatType(rs.getString("CHAT_TYPE"));
        imMsg.setSendUid(rs.getString("SEND_UID"));
        imMsg.setReceiveUid(rs.getString("RECEIVE_UID"));
        imMsg.setGroupId(rs.getString("GROUP_ID"));
        imMsg.setMsgType(rs.getString("MSG_TYPE"));
        imMsg.setMsg(rs.getString("MSG"));
        imMsg.setIfWithdraw(rs.getString("IF_WITHDRAW"));
        imMsg.setMsgShardingDate(rs.getString("MSG_SHARDING_DATE"));
        imMsg.setTime(rs.getTimestamp("TIME"));
        imMsg.setMsgExt(rs.getString("MSG_EXT"));
        imMsg.setMsgSeq(getInteger(rs, "MSG_SEQ"));
        imMsg.setFileName(rs.getString("FILE_NAME"));
        imMsg.setFileType(rs.getString("FILE_TYPE"));
        imMsg.setFileSize(rs.getString("FILE_SIZE"));
        imMsg.setFileExtension(rs.getString("FILE_EXTENSION"));
        imMsg.setFileLastmodified(rs.getString("FILE_LASTMODIFIED"));
        imMsg.setFileLastmodifieddate(rs.getTimestamp("FILE_LASTMODIFIEDDATE"));
        imMsg.setReferenceMsgId(getLong(rs,"REFERENCE_MSG_ID"));
        imMsg.setExt1(rs.getString("EXT1"));
        imMsg.setExt2(rs.getString("EXT2"));
        imMsg.setExt3(rs.getString("EXT3"));
        imMsg.setExt4(rs.getString("EXT4"));
        imMsg.setExt5(rs.getString("EXT5"));
        imMsg.setExt6(rs.getString("EXT6"));
        return imMsg;
    }
    private Long getLong(ResultSet rs, String columnName) throws SQLException {
        final Object object = rs.getObject(columnName);
        if (object != null) {
            return (Long) object;
        }
        return null;
    }

    private Integer getInteger(ResultSet rs, String columnName) throws SQLException {
        final Object object = rs.getObject(columnName);
        if (object != null) {
            return (Integer) object;
        }
        return null;
    }

}
